/**
 * [DragonApp.view.notice.ImageController description]
 * 图像公告窗体控制器
 */
Ext.define('DragonApp.view.notice.ImageController', {

    extend: 'DragonApp.view.foundation.ViewController',
    alias: 'controller.noticeimagegrid',

    //========
    //添加图像
    //========
    onAdd: function(button, e, options) {

        //当前记录
        var record = button.getWidgetRecord();
        var selectedStore = Ext.data.StoreManager.lookup('selectedStore');

        //记录克隆
        var cloneRecord = record.copy();

        //判断是否重复
        var res = selectedStore.queryRecords('path', cloneRecord.get('path'));
        if (res.length > 0) {
            return;
        } else {
            selectedStore.add(cloneRecord);
        }
    },
    //============
    //选择图库图像
    //============
    onImgSelect: function(button, state) {

        var win = button.up('window');
        var imagegrid = win.down('imagegrid');

        if (true == state) {
            win.setHeight(win.getHeight() + 250);
            imagegrid.setHidden(false);
            button.setText('隐藏图库');

        } else if (false == state) {
            imagegrid.setHidden(true);
            win.setHeight(win.getHeight() - 250);
            button.setText('选择图库');

        }
    },
    //图像上传
    onImageUpload: function(button, state) {

        var me = this,
            view = button.up('window');

        me.dialog = view.add({
            xtype: 'window',
            title: '图像上传',
            items: [{
                fieldLabel: '上传图像',
                xtype: 'fileuploadform',
                destUrl: DragonApp.ux.config.Config.getUrl('/index/Upload/uploadNotice/'),
                height: 210,
                width: 420
            }]
        });
        me.dialog.show();
    },
    //========
    //批量添加
    //========
    onBatImgSelect: function(button, e, options) {

        var cloneRecord;
        var grid = Ext.ComponentQuery.query('imagegrid')[0];
        var records = grid.getSelectionModel().getSelection();
        var selectedStore = Ext.data.StoreManager.lookup('selectedStore');

        records.forEach(function(record) {
            cloneRecord = record.copy();

            //判断是否重复
            var res = selectedStore.queryRecords('filename', cloneRecord.get('filename'));
            if (res.length > 0) {
                return;
            } else {
                selectedStore.add(cloneRecord);
            }
        });
    },

    //回车搜索
    onTextFieldSpecialKeySearch: function(field, e, options) {
        if (e.getKey() === e.ENTER) {
            this.onSearch();
        }
    },

    //==========
    //工具栏搜索
    //==========
    onSearch: function() {

        //搜索字段
        var searchFilter = Ext.ComponentQuery.query('notice-image-bar')[0];
        var searchField = searchFilter.queryById('search_field');

        //复位
        var store = Ext.data.StoreManager.lookup('imageNoticeStore');
        store.clearFilter();

        //内容过滤
        store.filterBy(function(item) {

            return (item.get('filename').indexOf(searchField.getValue()) >= 0);
        });
    },

    //已选择工具栏回车搜索
    onTextFieldSpecialKeySearchSelected: function(field, e, options) {
        if (e.getKey() === e.ENTER) {
            this.onSearchSelected();
        }
    },

    //==========
    //工具栏搜索
    //==========
    onSearchSelected: function() {

        //搜索字段
        var searchFilter = Ext.ComponentQuery.query('notice-selected-bar')[0];
        var searchField = searchFilter.queryById('search_field');

        //复位
        var store = Ext.data.StoreManager.lookup('selectedStore');
        store.clearFilter();

        //内容过滤
        store.filterBy(function(item) {

            return (item.get('filename').indexOf(searchField.getValue()) >= 0);
        });
    },
    //========
    //图像删除
    //========
    onDelete: function(button, e, options) {

        var record = button.getWidgetRecord();
        record.drop();
    },
    //========
    //图像预览
    //========
    onPreview: function(button, e, options) {

        //当前记录
        var record = button.getWidgetRecord();
        var path = record.get('path');
        var me = this,
            view = me.getView();

        me.dialog = view.add({
            xtype: 'window',
            title: record.get('filename') + ' 预览',
            width: record.get('width') + 2,
            height: record.get('height') + 37,
            layout: 'fit',
            html: '<img src="' + DragonApp.ux.config.Config.getNoticeUrl(record.get('filename')) + '" alt="' + record.get('filename') + '" />'
        });
        me.dialog.show();
    },
    //========
    //发布公告
    //========
    onSubmit: function(button, e, options) {

        var selectedStore = Ext.data.StoreManager.lookup('selectedStore');
        var arr = [];

        for (var i = 0; i < selectedStore.getCount(); i++) {
            filename = selectedStore.getAt(i).get('filename');
            arr.push(filename);
        }
        //图像选择提示
        if (0 == arr.length) {
            DragonApp.util.Msg.showToast('请上传/选择公告图像');
            return;
        }
        var pictures = arr.join();

        //获取表单
        var form = button.up('window').down('form').getForm();
        if (!form.isValid()) return;

        //表单取值
        var formValue = form.getValues();

        formValue.pictures = pictures;
        formValue.channels = formValue.channel.join();

        //网址提交
        var url = ('new' == formValue.formtype) ? DragonApp.ux.config.Config.getUrl('/index/Notice/add/') : DragonApp.ux.config.Config.getUrl('/index/Notice/update/');

        form.submit({

            url: url,
            waitMsg: '发布之中...',
            params: formValue,

            success: function(form, action) {

                if (true == action.result.success) {
                    //图库增加记录
                    var imgNoticeStore = Ext.data.StoreManager.lookup('jsonPNoticeStore');
                    imgNoticeStore.reload();

                    //成功提示
                    DragonApp.util.Msg.showToast(action.result.msg);

                    //关闭窗体
                    var win = Ext.ComponentQuery.query('notice-image-window')[0];
                    win.destroy();
                } else {
                    //失败提示
                    DragonApp.util.Msg.showToast(action.result.msg);
                }

            },
            failure: function(form, action) {

                switch (action.failureType) {
                    case Ext.form.action.Action.CLIENT_INVALID:
                        Ext.Msg.alert('提示', 'Form fields may not be submitted with invalid values');
                        break;
                    case Ext.form.action.Action.CONNECT_FAILURE:
                        Ext.Msg.alert('提示', 'Ajax communication failed');
                        break;
                    case Ext.form.action.Action.SERVER_INVALID:
                        Ext.Msg.alert('提示', action.result.msg);
                }
            }
        });
    }
});